home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / comms / other / ftransapiv1.3 / sourcecode / ftrans.c < prev    next >
C/C++ Source or Header  |  1999-11-30  |  14KB  |  476 lines

  1. #include "surround.h"
  2. #include <exec/types.h>
  3. #include <intuition/intuition.h>
  4. #include <intuition/IntuitionBase.h>
  5. #include <intuition/classusr.h>
  6. #include <intuition/pointerclass.h>
  7. #include <intuition/gadgetclass.h>
  8. #include <intuition/sghooks.h>
  9. #include <intuition/icclass.h>
  10. #include <graphics/displayinfo.h>
  11. #include <graphics/text.h>
  12. #include <graphics/gfx.h>
  13. #include <libraries/dos.h>
  14. #include <libraries/gadtools.h>
  15. #include <libraries/diskfont.h>
  16. #include <libraries/asl.h>
  17. #include <clib/exec_protos.h>
  18. #include <clib/intuition_protos.h>
  19. #include <clib/dos_protos.h>
  20. #include <clib/graphics_protos.h>
  21. #include <clib/gadtools_protos.h>
  22. #include <clib/asl_protos.h>
  23. #include <clib/datatypes_protos.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <dos/dos.h>
  28. #include <dos/dosasl.h>
  29. #include <exec/exec.h>
  30. #include <proto/exec.h>
  31. #include <proto/utility.h>
  32. #include <proto/datatypes.h>
  33. #include <datatypes/datatypesclass.h>
  34. #include <datatypes/pictureclass.h>
  35. #include <datatypes/animationclass.h>
  36. #include <datatypes/datatypes.h>
  37. #include <errno.h>
  38. #include <netdb.h>
  39. #include <sys/types.h>
  40. #include <sys/time.h>
  41. #include <sys/socket.h>
  42. #include <sys/ioctl.h>
  43. #include <netinet/in.h>
  44. #include <proto/socket.h>
  45. #include <signal.h>
  46.  
  47. // INFORMACOES DA VERSAO
  48. #define VERS "Freedom Internet Translator Client API v1.3"
  49. #define DATE "25.11.1999"
  50. #define VSTRING VERS " (" DATE ")"
  51. #define VERSTAG "\\0$VER: " VERS " (" DATE ")"
  52. const static char VersTag[] = VERSTAG;
  53.  
  54. // NUMERO MAXIMO DE SOCKETS ABERTOS
  55. #define MAXSOCKS 10L
  56.  
  57. // TAMANHO DO BUFFER DE LEITURA DE COMUNICACAO
  58. int TempBufferSize=65535;
  59.  
  60. // PONTO DE ACESSO A UM HOST
  61. struct hostent *hptr;
  62. struct sockaddr_in server;
  63. int byteslidos,result,totalbytes;
  64. unsigned char  *enderecoip;
  65. BPTR ArquivoCache=NULL;
  66. char *TempBuffer=NULL;
  67. char TempString[256], status[256];
  68. LONG TipoEndereco;
  69. char NomeDoHost[256];
  70. char direcao[50];
  71. int socket1;
  72. char opts[] = "ftudo";
  73.  
  74. void top(char servidor[35])
  75. {
  76.   printf("\nFreedom Internet Translator API v1.3 [25.11.1999]\n");
  77.   printf("Server: %s\n", servidor);
  78.   printf("By Cristian Robert Gallas (SuRRouND)\n");
  79.   printf("Lithium Software 1999(r)\n");
  80.   printf("FREEWARE WITH SOURCE FREE - CONTRIBUTE TO AMIGA OPEN SOURCE!\n\n");
  81.   return;
  82. }
  83.  
  84. void syntax(void)
  85. {
  86.   printf("Syntax:\n");
  87.   printf("-f[filename] Translate a File\n");
  88.   printf("-t[text]     Translate a Text\n");
  89.   printf("-u[url]      Translate a Url\n");
  90.   printf("-d[x]        Translate direction\n");
  91.   printf("-o[filename] Output file to url translation (ram:url.html default)\n");
  92.   printf("nofilters    Disabled text filters\n");
  93.   printf("noremove     Dont remove the output file from ram, to debug!\n");
  94.   printf("silence      Dont show status messages\n");
  95.   printf("silencemax   Show only translated text\n");
  96.   printf("server1      translator.go.com (default)\n");
  97.   printf("server2      babelfish.altavista.digital.com\n");
  98.   printf("help         Show this help ;)\n\n");
  99.   printf("Translate To: 0 - English   to Francais\n");
  100.   printf("              1 - English   to Deutsch\n");
  101.   printf("              2 - English   to Italiano\n");
  102.   printf("              3 - English   to Portugues\n");
  103.   printf("              4 - English   to Espanol\n");
  104.   printf("              5 - Francais  to English\n");
  105.   printf("              6 - Deutsch   to English\n");
  106.   printf("              7 - Italiano  to English\n");
  107.   printf("              8 - Espanol   to English\n");
  108.   printf("              9 - Portugues to English\n\n");
  109.   printf("Examples:\n");
  110.   printf("FTrans -f \"ram:text.txt\" -d 3 silence\n");
  111.   printf("FTrans -u \"http:\\www.microsoft.com\" -o \"ram:urltrans.html\" -d 3 nofilters\n");
  112.   printf("FTrans -t \"Translate This!\" -d 2\n");
  113.   printf("FTrans -f \"sys:amiga.txt\" -d 2 silencemax server2\n");
  114.   printf("\n");
  115.   return;
  116. }
  117.  
  118. void main(int argc, char *argv[])
  119. {
  120.   char *linha, *linhaf, *requisicao,
  121.        opcao[10], lingua[10], tamanho[50], a,
  122.        *saida, *teste, tmpfile[60], urlfile[256],
  123.        arquivo[256], servidor[35], option,*odata;
  124.   long int x = 0;
  125.   int iservidor = 0, dir = 0, next, silence = 0,
  126.       filter = 0, url = 0, remtmp = 0, max = 0;
  127.   FILE *entrada;
  128.  
  129.  
  130.   // SETA SERVER DEFAULT E URLFILE DEFAULT
  131.   iservidor = 0;
  132.   strcpy(servidor, "translator.go.com");
  133.   strcpy(urlfile,  "ram:url.html");
  134.  
  135.   // ACERTA O NOME DO ARQUIVO TEMPORARIO
  136.   sprintf(tmpfile, "Ram:FreeTmp.%s", DataHora(7));
  137.  
  138.   // INICIA OS TESTES DOS COMANDOS COM ARGUMENTOS
  139.   next = 1;
  140.   while((odata = argopt(argc, argv, opts, &next, &option)) != NULL)
  141.   {
  142.     if (toupper(option) == 'F') {
  143.       strcpy(opcao, "file");
  144.       strcpy(arquivo, odata); }
  145.  
  146.     if (toupper(option) == 'T') {
  147.       strcpy(opcao, "text");
  148.       linhaf = malloc(strlen(odata));
  149.       strcpy(linhaf, odata); }
  150.  
  151.     if (toupper(option) == 'U') {
  152.       strcpy(opcao, "url");
  153.       linhaf = malloc(strlen(odata));
  154.       strcpy(linhaf, odata);
  155.       // FAZ ALGUNS FILTROS NA URL
  156.       strcpy(linhaf, TrimSur(linhaf, 0));
  157.       strcpy(linhaf, SubstituiString(strlwr(linhaf), "http://", ""));
  158.       strcpy(linhaf, SubstituiString(strlwr(linhaf), "http:", ""));
  159.       url = 1; }
  160.  
  161.     if (toupper(option) == 'D') {
  162.       dir = atoi(odata); }
  163.  
  164.     if (toupper(option) == 'O') {
  165.       strcpy(urlfile, odata); }
  166.   }
  167.  
  168.   // INICIA O TESTE COM OS ARGUMENTOS SOZINHOS
  169.   for (; next < argc; next++)
  170.   {
  171.     // TESTA OPCAO SILENCE
  172.     if(!strcmp(strupr(argv[next]), "SILENCE"))
  173.       silence = 1;
  174.     // TESTA OPCAO HELP
  175.     if(!strcmp(strupr(argv[next]), "HELP")) {
  176.       top(servidor);
  177.       syntax();
  178.       exit(0); }
  179.     // TESTA OPCAO NOFILTERS
  180.     if(!strcmp(strupr(argv[next]), "NOFILTERS"))
  181.       filter = 1;
  182.     // SETA SERVER 1 TRANSLATOR.GO
  183.     if(!strcmp(strupr(argv[next]), "SERVER1")) {
  184.       iservidor = 0;
  185.       strcpy(servidor, "translator.go.com"); }
  186.     // SETA SERVER 1 TRANSLATOR.GO
  187.     if(!strcmp(strupr(argv[next]), "SERVER2")) {
  188.       iservidor = 1;
  189.       strcpy(servidor, "babelfish.altavista.digital.com"); }
  190.     // PARAMETRO PARA NAO REMOVER O ARQUIVO TMP
  191.     if(!strcmp(strupr(argv[next]), "NOREMOVE")) {
  192.       remtmp = 1; }
  193.     // PARAMETRO PARA MOSTRAR SOMENTE O TEXTO TRADUZIDO
  194.     if(!strcmp(strupr(argv[next]), "SILENCEMAX")) {
  195.       silence = 1;
  196.       max     = 1; }
  197.   }
  198.  
  199.   // TESTA SE TEM INFORMACOES SUFICIENTES PARA PROCESSAR
  200.   if(argc < 3) {
  201.     top(servidor);
  202.     syntax();
  203.     exit(0); }
  204.  
  205.   // VERIFICA A LINGUA A TRADUZIR
  206.   switch (dir)
  207.   {
  208.     case 0: strcpy(lingua, "en_fr"); 
  209.             strcpy(direcao, "English to Francais"); break;
  210.     case 1: if(iservidor == 0)
  211.               strcpy(lingua, "en_ge");
  212.             else
  213.               strcpy(lingua, "en_de");
  214.             strcpy(direcao, "English to Deutsch"); break;
  215.     case 2: strcpy(lingua, "en_it");
  216.             strcpy(direcao, "English to Italiano"); break;
  217.     case 3: strcpy(lingua, "en_pt");
  218.             strcpy(direcao, "English to Portugues"); break;
  219.     case 4: if(iservidor == 0)
  220.               strcpy(lingua, "en_sp");
  221.             else
  222.               strcpy(lingua, "en_es");
  223.             strcpy(direcao, "English to Espanol"); break;
  224.     case 5: strcpy(lingua, "fr_en");
  225.             strcpy(direcao, "Francais to English"); break;
  226.     case 6: if(iservidor == 0)
  227.               strcpy(lingua, "ge_en");
  228.             else
  229.               strcpy(lingua, "de_en");
  230.             strcpy(direcao, "Deutsch to English"); break;
  231.     case 7: strcpy(lingua, "it_en");
  232.             strcpy(direcao, "Italiano to English"); break;
  233.     case 8: if(iservidor == 0)
  234.               strcpy(lingua, "sp_en");
  235.             else
  236.               strcpy(lingua, "es_en");
  237.             strcpy(direcao, "Espanol to English"); break;
  238.     case 9: strcpy(lingua, "pt_en");
  239.             strcpy(direcao, "Portugues to English"); break;
  240.     default:strcpy(lingua, "en_pt");
  241.             strcpy(direcao, "English to Portugues"); break;
  242.   }
  243.  
  244.  
  245.   // ROTINA QUE LE O CONTEUDO DE UM ARQUIVO TX 
  246.   if(!strcmp(strlwr(opcao), "file"))
  247.   {
  248.     // ROTINA QUE LE O ARQUIVO E JOGA NA VARIAVEL LINHA
  249.     if(!(entrada = fopen(arquivo, "r")))
  250.     {
  251.       printf("%s: Access denied or file not found...\n\n", arquivo);
  252.       goto SAIDAGOTO;
  253.     }
  254.     // ALOCA MEMORIA PARA AS VARIAVEIS DE PEDIDO
  255.     x = atof(ExaminaArquivo(arquivo, 0));
  256.     linhaf    = malloc(x);
  257.     linhaf[0] = '\0';
  258.  
  259.     // LE O ARQUIVO E JOGA NA VARIAVEL PARA TRADUCAO
  260.     x = 0;
  261.     while(a = getc(entrada),! feof(entrada))
  262.       linhaf[x++] = a;
  263.     linhaf[x] = '\0';
  264.     fclose(entrada);
  265.   } 
  266.  
  267.   // ALOCA VARIAVEIS DE AUXILIO
  268.   linha      = malloc(strlen(linhaf) + 500);
  269.   requisicao = malloc(strlen(linhaf) + 500);
  270.   linha[0] = '\0'; requisicao[0] = '\0';
  271.  
  272.   // TITULO DO PROGRAMA
  273.   if(silence == 0)
  274.     top(servidor);
  275.  
  276.   // INICIA A CONECCAO NO SERVIDOR ABRINDO A SOCKET
  277.   SockBase=(struct Library *)OpenLibrary("socket.library",0L);
  278.   if(!(SockBase))
  279.   {
  280.     printf("You need connect on internet first...\n\n");
  281.     goto SAIDAGOTO;
  282.   }
  283.   if(silence == 0)
  284.     printf("Open Socket...\n");
  285.  
  286.   setup_sockets(MAXSOCKS,&errno);
  287.  
  288.   // PEGA INFORMACOES SOBRE O HOST
  289.   if(!(hptr=gethostbyname(servidor)))
  290.   {
  291.     printf("Problem on translator server, try again later...\n\n");
  292.     goto SAIDAGOTO;
  293.   }
  294.   memcpy((char *)&server.sin_addr,(char *)hptr->h_addr,hptr->h_length);
  295.   // AF_INET = INTERNET
  296.   server.sin_family=AF_INET;
  297.  
  298.   // PORTA 80 (SERVIDOR WEB)
  299.   server.sin_port=80;
  300.  
  301.   TipoEndereco=hptr->h_addrtype;
  302.   strcpy(NomeDoHost,hptr->h_name);
  303.   enderecoip=(unsigned char *)hptr->h_addr_list[0];
  304.   socket1=socket(hptr->h_addrtype, SOCK_STREAM,0);
  305.  
  306.   if(silence == 0)
  307.     printf("Contacting host...\n");
  308.  
  309.   // CONECTA O SOCKET AO ENDERECO
  310.   result=connect(socket1,(struct sockaddr*)&server,sizeof(server));
  311.   if(result == -1)
  312.   {
  313.     printf("Connection problem, try again later...\n\n");
  314.     goto SAIDAGOTO;
  315.   }
  316.   if(silence == 0)
  317.     printf("Host contacted. Now Doing request...\n");
  318.  
  319.   // ROTINA QUE MONTA O PEDIDO DE TRADUCAO
  320.   if(url == 0)
  321.     if(iservidor == 0)
  322.       sprintf(linha, "lp=%s&input_type=text&text=%s", lingua, linhaf);
  323.     else
  324.       sprintf(linha, "doit=done&urltext='%s'&languagepair=%s", linhaf, lingua);
  325.   else
  326.     if(iservidor == 0)
  327.       sprintf(linha, "lp=%s&input_type=url&url=%s", lingua, linhaf);
  328.     else
  329.       sprintf(linha, "doit=done&urltext=http://%s&languagepair=%s", linhaf, lingua);
  330.  
  331.   sprintf(tamanho, "Content-Length: %d", strlen(linha));
  332.  
  333.   // INICIALIZA A REQUISICAO
  334.   requisicao[0]='\0';
  335.   if (iservidor ==  0)
  336.     strcpy(requisicao,"POST / HTTP/1.0");
  337.   else
  338.     strcpy(requisicao,"POST /cgi-bin/translate? HTTP/1.0");
  339.   strcat(requisicao, "\x0d\x0a\0");
  340.   strcat(requisicao, "User-Agent: FreedomTranslator/1.2; (Amiga Translator Client)");
  341.   strcat(requisicao, "\x0d\x0a\0");
  342.   strcat(requisicao, "Accept: */*;q=1");
  343.   strcat(requisicao, "\x0d\x0a\0");
  344.   strcat(requisicao, "Host: ");
  345.   strcat(requisicao, servidor);
  346.   strcat(requisicao, "\x0d\x0a\0");
  347.   strcat(requisicao, tamanho);
  348.   strcat(requisicao, "\x0d\x0a\0");
  349.   strcat(requisicao, "Content-Type: application/x-www-form-urlencoded");
  350.   strcat(requisicao, "\x0d\x0a\0");
  351.   strcat(requisicao, "\x0d\x0a\0");
  352.   strcat(requisicao, linha);
  353.   strcat(requisicao, "\x0d\x0a\0");
  354.   strcat(requisicao, "\x0d\x0a\0");
  355.  
  356.   // ENVIA A REQUISICAO DE HTTP
  357.   result=send(socket1,requisicao,strlen(requisicao),0);
  358.   if(silence == 0)
  359.     printf("Done http request. Waiting... ");
  360.  
  361.   totalbytes=0;
  362.   if(!(ArquivoCache = Open(tmpfile, MODE_NEWFILE)))
  363.   {
  364.     printf("FTranslator port is open, binary FTranslator is in use, wait...\n\n");
  365.     goto SAIDAGOTO;
  366.   }
  367.   TempBuffer=AllocMem(TempBufferSize,MEMF_FAST|MEMF_CLEAR);
  368.   if(!(TempBuffer))
  369.   {
  370.     printf("Not have memory for allocate temporary buffer...\n");
  371.     goto SAIDAGOTO;
  372.   }
  373.   do
  374.   {
  375.     byteslidos  = 0;
  376.     *TempBuffer = '\0';
  377.     byteslidos  = recv(socket1, TempBuffer, TempBufferSize-1,0);
  378.     if(byteslidos)
  379.     {
  380.       Write(ArquivoCache, TempBuffer, byteslidos);
  381.       totalbytes += byteslidos;
  382.       TempString[0] = '\0';
  383.     }
  384.   }
  385.   while(byteslidos>0);
  386.  
  387.   // FECHA O SOCKET E O ARQUIVO
  388.   s_close(socket1);
  389.   Close(ArquivoCache);
  390.   FreeMem(TempBuffer,TempBufferSize);
  391.   cleanup_sockets();
  392.   CloseLibrary((struct Library *)SockBase);
  393.  
  394.   // ETAPA FINAL DO PROGRAMA
  395.   if(silence == 0)
  396.     printf("Done.\n");
  397.  
  398.   if(!(ExisteArquivo(tmpfile)))
  399.   {
  400.     printf("%s: Problem on temporary file...\n\n", tmpfile);
  401.     goto SAIDAGOTO;
  402.   }
  403.  
  404.   if(url == 0)
  405.   {
  406.     saida=malloc(atoi(ExaminaArquivo(tmpfile, 0)) + 500);
  407.     teste=malloc(atoi(ExaminaArquivo(tmpfile, 0)) + 500);
  408.  
  409.     if(iservidor == 0)
  410.       strcpy(saida, CortaStringArquivo(tmpfile, "<textarea name=\"transtext\" cols=\"52\" rows=\"20\" wrap=\"VIRTUAL\">", "</textarea>", 2));
  411.     else
  412.       strcpy(saida, CortaStringArquivo(tmpfile, "<font face=\"arial, helvetica\">", "<br><br></font></td></tr>", 2));
  413.   } else {
  414.     sprintf(saida, "Translate Url, Save on: %s\n", urlfile);
  415.   }
  416.  
  417.   // FILTRA LINHA PARA MELHORAR A SAIDA DA TRADUCAO
  418.   if(filter == 0 && url == 0)
  419.   {
  420.     if(silence == 0)
  421.       printf("Applying filters... ");
  422.  
  423.     strcpy(teste, SubstituiString(saida, "<P>", "\n"));
  424.     strcpy(saida, teste);
  425.     strcpy(teste, SubstituiString(saida, "<", ""));
  426.     strcpy(saida, teste);
  427.     strcpy(teste, SubstituiString(saida, ">", ""));
  428.     strcpy(saida, teste);
  429.     strcpy(teste, SubstituiCaracter(saida, '`', ' '));
  430.     strcpy(saida, teste);
  431.     strcpy(teste, SubstituiCaracter(saida, '\'', ' '));
  432.     strcpy(saida, teste);
  433.     strcpy(teste, SubstituiCaracter(saida, '\"', '^'));
  434.     strcpy(saida, teste);
  435.     strcpy(teste, SubstituiCaracter(saida, '&', 'e'));
  436.     strcpy(saida, teste);
  437.   }
  438.  
  439.   if(silence == 0)
  440.     printf("Done.\n\n");
  441.  
  442.   // MOSTRA A TRADUCAO
  443.   if (max == 0)
  444.   {
  445.     printf(" Translation: %s\n", direcao);
  446.     printf("o----------------------------------------------o\n");
  447.   }
  448.  
  449.   // MENSAGEM TRADUZIDA
  450.   printf("%s\n", saida);
  451.  
  452.   if (max == 0)
  453.   {
  454.     printf("o----------------------------------------------o\n");
  455.     printf(" FTranslator API by Cristian Robert Gallas v1.3 \n");
  456.     printf(" Freeware with Source Free  (Amiga Open Source) \n");
  457.     printf("o----------------------------------------------o\n");
  458.   }
  459.  
  460.   // FINALIZA O PROGRAMA
  461.   SAIDAGOTO:
  462.   free(linhaf); free(linha); free(requisicao); free(saida);
  463.   free(teste);
  464.  
  465.   if(url == 0)
  466.   {
  467.     if(remtmp == 0) {
  468.       remove(tmpfile); }
  469.   } else {
  470.     remove(urlfile);
  471.     rename(tmpfile, urlfile);
  472.   }
  473.  
  474.   exit(0);
  475. }                                           
  476.